package com.ecoolex.chinadutyfree.retailer.core.manager;

import java.time.Instant;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ecoolex.chinadutyfree.retailer.core.dao.GoodsCategoryMarketingDAO;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsCategoryMarketing;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsCategoryMarketing.Column;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsCategoryMarketingExample;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsCategoryMarketingExample.Criteria;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;

@Service
public class GoodsCategoryMarketingManager {
	
    @Autowired
    private GoodsCategoryMarketingDAO dao;
    
    public void verify(GoodsCategoryMarketing record) {
    	BizAssert.isTrue(Check.notNullAndGtZero(record.getCategoryId()),ResultCode.BIZ.build(10), "分类id为空");
    	BizAssert.isTrue(Check.notNullAndGtZero(record.getSorting()),ResultCode.BIZ.build(11), "排序为空");
    }
    
    public GoodsCategoryMarketing add(GoodsCategoryMarketing record, String createUser) {
    	verify(record);
    	BizAssert.isTrue(Check.isNull(getByCategoryId(record.getCategoryId())),ResultCode.BIZ.build(12), "分类已经配置");
    	record.setDeleted(GoodsCategoryMarketing.NOT_DELETED);
        record.setCreateUser(createUser);
        record.setCreateTime(Date.from(Instant.now()));
        record.setUpdateUser(createUser);
        record.setUpdateTime(Date.from(Instant.now()));

        dao.insert(record);
        return record;
    }
    
    public GoodsCategoryMarketing edit(GoodsCategoryMarketing record, String updateUser) {
    	verify(record);
    	record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(record, Column.sorting,Column.updateTime,Column.updateUser);
        return record;
    }
    
    public void delete(GoodsCategoryMarketing record, String updateUser) {
    	record.setDeleted(GoodsCategoryMarketing.IS_DELETED);
    	record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(record, Column.deleted,Column.updateTime,Column.updateUser);
    }
    
    public GoodsCategoryMarketing get(Integer id) {
    	return dao.selectByPrimaryKeyWithLogicalDelete(id, GoodsCategoryMarketing.NOT_DELETED);
    }
    
    public GoodsCategoryMarketing getByCategoryId(Integer categoryId) {
    	GoodsCategoryMarketingExample example = new GoodsCategoryMarketingExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andCategoryIdEqualTo(categoryId).andDeletedEqualTo(GoodsCategoryMarketing.NOT_DELETED);
    	return dao.selectOneByExample(example);
    }
    
    public Pagination<GoodsCategoryMarketing> query(String name,Sorting[] sorts, Page page){
    	return Pagination.create(page, 
    		dao.countByName(name),
    		() -> dao.queryByName(name, Sortings.builder(sorts).verify(Column::valueOf).toSql(), page));
    }
    
    public Pagination<GoodsCategoryMarketing> queryForH5(Page page){
    	return Pagination.create(page, 
    		dao.count(),
    		() -> dao.query(page));
    }
    
    public GoodsCategoryMarketing getForH5(Integer id) {
    	return dao.getForH5(id);
    }
}